/* 
题目1：编写一个 People 类，使其的实例具有监听事件、触发事件、解除绑定功能。（实例可能监听多个不同的事件，也可以去除监听事件）
*/
class People {
    constructor(name) {
        this.name = name
        this.eventObj={}
    }

    // TODO: 请在此处完善代码
    // 监听事件
    on(event,cb){
        // 挂载事件
        if(this.eventObj[event]){
            this.eventObj[event].push(cb)
        }else{
            this.eventObj[event]=[cb]
        }
        
    }
    // 触发事件
    emit(event,mes){
        this.eventObj[event].map(cb=>{
            cb(mes)
        })
    }
    // 解绑事件
    off(event,cb){
        this.eventObj[event]=this.eventObj[event].filter(item=>{
            return !(item===cb)
        })
    }
    sayHi() {
        console.log(`Hi, I am ${this.name}`)
    }
}

/* 以下为测试代码 */
const say1 = (greeting) => {
    console.log(`${greeting}, nice meeting you.`)
}

const say2 = (greeting) => {
    console.log(`${greeting}, nice meeting you, too.`)
}

const jerry = new People('Jerry')
jerry.sayHi()
// => 输出：'Hi, I am Jerry'

jerry.on('greeting', say1)
jerry.on('greeting', say2)
jerry.emit('greeting', 'Hi')
// => 输出：'Hi, nice meeting you.' 和 'Hi, nice meeting you, too'

jerry.off('greeting', say1)
console.log(jerry);

jerry.emit('greeting', 'Hi')
// => 只输出：'Hi, nice meeting you, too'

